package leetcode.concurrency;

import java.util.concurrent.Semaphore;
import java.util.function.IntConsumer;

/**
 * 打印零与奇偶数
 * @author jiangwz
 * @create 2019/11/6.
 */
public class Q1116 {

    public static void main(String[] args) {
        try {
            ZeroEvenOdd zeroEvenOdd = new ZeroEvenOdd(5);
            IntConsumer intConsumer = value -> System.out.print(value);

            new Thread(() -> {
                try {
                    zeroEvenOdd.zero(intConsumer);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }, "1").start();
            new Thread(() -> {
                try {
                    zeroEvenOdd.even(intConsumer);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }, "2").start();
            new Thread(() -> {
                try {
                    zeroEvenOdd.odd(intConsumer);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }, "3").start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

//class ZeroEvenOdd {
//
//    // 完全用阻塞队列实现
//    BlockingQueue<Integer> even = new LinkedBlockingQueue<>();
//    BlockingQueue<Integer> odd = new LinkedBlockingQueue<>();
//    BlockingQueue<Integer> zero = new LinkedBlockingQueue<>();
//
//
//    private int n;
//
//    public ZeroEvenOdd(int n) {
//        this.n = n;
//    }
//
//    // printNumber.accept(x) outputs "x", where x is an integer.
//    public void zero(IntConsumer printNumber) throws InterruptedException {
//        for (int i = 1; i <= n; i++) {
//            printNumber.accept(0);
//            if (i%2==1){
//                odd.add(1);
//            }else {
//                even.add(1);
//            }
//            zero.take();
//        }
//    }
//
//    public void even(IntConsumer printNumber) throws InterruptedException {
//        for (int i = 1; i <= n; i++) {
//            if (i%2 == 0) {
//                even.take();
//                printNumber.accept(i);
//                zero.add(1);
//            }
//        }
//
//    }
//
//    public void odd(IntConsumer printNumber) throws InterruptedException {
//        for (int i = 1; i <= n; i++) {
//            if (i%2==1){
//                odd.take();
//                printNumber.accept(i);
//                zero.add(1);
//            }
//        }
//    }
//}


// semaphore
class ZeroEvenOdd {

    // 完全用阻塞队列实现
    Semaphore zero = new Semaphore(0);
    Semaphore even = new Semaphore(0);
    Semaphore odd = new Semaphore(0);

    private int n;

    public ZeroEvenOdd(int n) {
        this.n = n;
    }

    // printNumber.accept(x) outputs "x", where x is an integer.
    public void zero(IntConsumer printNumber) throws InterruptedException {
        for (int i = 1; i <= n; i++) {
            printNumber.accept(0);
            if (i%2==1){
                odd.release();
            }else {
                even.release();
            }
            zero.acquire();
        }
    }

    public void even(IntConsumer printNumber) throws InterruptedException {
        for (int i = 1; i <= n; i++) {
            if (i%2 == 0) {
                even.acquire();;
                printNumber.accept(i);
                zero.release();
            }
        }

    }

    public void odd(IntConsumer printNumber) throws InterruptedException {
        for (int i = 1; i <= n; i++) {
            if (i%2==1){
                odd.acquire();
                printNumber.accept(i);
                zero.release();
            }
        }
    }
}